<template>
  <a-card :bordered="false">

    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="0">
          <a-col :md="6" :sm="24">
            <a-form-item label="声卡名称">
              <a-input v-model="queryParam.cardName" allow-clear placeholder="请输入声卡名称"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <span class="table-page-search-submitButtons" style="padding-left: 16px;">
              <a-form-item label="唯一编码">
                <a-input v-model="queryParam.code" allow-clear placeholder="请输入唯一编码"/>
              </a-form-item>
            </span>
          </a-col>
          <a-col :md="6" :sm="24">
            <span class="table-page-search-submitButtons" style="padding-left: 16px;">
              <a-button type="primary" class="btn_search_reset" @click="$refs.table.refresh(true)"><a-icon
                type="search"/>查询</a-button>
              <a-button
                style="margin-left: 8px"
                class="btn_search_reset"
                @click="() => {queryParam = {};$refs.table.refresh(true)}"><a-icon type="reload"/>重置</a-button>
            </span>
          </a-col>
          <a-col :md="6" :sm="24">
            <span class="table-page-search-submitButtons" style="float: right;">
              <a-button type="primary" class="btn_add" icon="plus" @click="$refs.addForm.add()">新增声卡</a-button>
            </span>
          </a-col>
        </a-row>

      </a-form>
    </div>
    <s-table
      ref="table"
      size="default"
      :columns="columns"
      :data="loadData"
      :alert="true"
      id="table-wrapper"
      :rowKey="(record) => record.id"
    >
      <span slot="action" slot-scope="text, record">
        <a @click="$refs.editForm.edit(record)" class="btn_edit">编辑</a>
        <a-divider type="vertical"/>
        <a-popconfirm class="btn_del" placement="topRight" title="确认删除？" @confirm="() => del(record)">
          <a>删除</a>
        </a-popconfirm>
      </span>

    </s-table>

    <add-form ref="addForm" @ok="handleOk"/>
    <edit-form ref="editForm" @ok="handleOk"/>

  </a-card>
</template>

<script>

  import { STable } from '@/components'
  import { page, del } from '@/api/modular/main/soundCard'
  import addForm from './addForm'
  import editForm from './editForm'

  export default {
    components: {
      STable,
      addForm,
      editForm
    },

    data () {
      return {
        // 查询参数
        queryParam: {},
        // 表头
        columns: [
          {
            title: '声卡名称',
            dataIndex: 'cardName'
          },
          {
            title: '唯一编码',
            dataIndex: 'code'
          },
          {
            title: '声卡识别名称',
            dataIndex: 'distinguishCode'
          },
          {
            title: '通道数量',
            dataIndex: 'vocalTractNumber'
          },
          {
            title: '创建时间',
            dataIndex: 'createTime',
            align: 'center',
            width: 150
          },
          {
            title: '操作',
            dataIndex: 'action',
            align: 'center',
            scopedSlots: { customRender: 'action' },
            width: 150
          }
        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          try {
            return page(Object.assign(parameter, this.queryParam)).then((res) => {
              return res.data
            })
          } catch (e) {
          }
        },
        selectedRowKeys: [],
        selectedRows: []
      }
    },

    created () {
      if (this.hasPerm('sysRole:edit') || this.hasPerm('sysRole:grantMenu') || this.hasPerm('sysRole:grantData') || this.hasPerm('sysRole:delete')) {
        this.columns.push({
          title: '操作',
          width: '150px',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' }
        })
      }
    },

    methods: {
      del (record) {
        del(record).then((res) => {
          if (res.success) {
            this.$message.success('删除成功')
            this.$refs.table.refresh()
          } else {
            this.$message.error('删除失败：' + res.message)
          }
        }).catch((err) => {
          this.$message.error('删除错误：' + err.message)
        })
      },
      handleOk () {
        this.$refs.table.refresh()
      },
      onSelectChange (selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      }
    }

  }
</script>

<style lang="less">
  .table-operator {
    margin-bottom: 18px;
  }

  button {
    margin-right: 8px;
  }

</style>
